import { takeEvery, delay, put, select } from 'redux-saga/effects'
import { searchStudents } from '../../services/student'
import { asyncDecrease, asyncIncrease, increase, decrease } from '../actions/counter'
import { fetchStudents, setStudents, loading } from '../actions/student/searchResult'

/**
 * saga 任务
 * @export
 */
export default function* () {
    yield takeEvery(asyncDecrease.toString(), asyncDecreaseSaga)
    yield takeEvery(asyncIncrease.toString(), asyncIncreaseSaga)
    yield takeEvery(fetchStudents.toString(), fetchStudentsSaga)
}

function* asyncDecreaseSaga() {
    yield delay(1000)
    yield put(decrease())
}

function* asyncIncreaseSaga() {
    yield delay(1000)
    yield put(increase())
}

// 获取学生数据
function* fetchStudentsSaga() {
    const condition = yield select(state => state.student.condition)
    yield put(loading(true))
    const result = yield searchStudents(condition)
    yield put(setStudents(result))
    yield put(loading(false))
}